package com.sgb.capital.view.pop;

import android.content.Context;
import android.text.Editable;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.TextView;

import com.sgb.capital.R;
import com.sgb.capital.callback.Constants;
import com.sgb.capital.callback.MyZTextWatcher;
import com.sgb.capital.model.BusinessInfoBean;
import com.sgb.capital.utils.Utils;
import com.sgb.capital.view.ui.adapter.DataEnterpriseAdapter;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import razerdp.basepopup.BasePopupWindow;
import razerdp.util.animation.AnimationHelper;
import razerdp.util.animation.Direction;
import razerdp.util.animation.TranslationConfig;

/**
 * 作者:张磊
 * 日期:2021/4/16 0016
 * 说明:企业列表
 */
public class EnterpriseListPop extends BasePopupWindow {
    public List<BusinessInfoBean> mDatas = new ArrayList<>();
    public List<BusinessInfoBean> mSsDatas = new ArrayList<>();
    public DataEnterpriseAdapter mAdapter;
    public BusinessInfoBean mData;
    public boolean isClick;
    public int mLastIndex = 0;
    private EditText mEtSs;
    public View mIvNull;

    public EnterpriseListPop(Context context) {
        super(context);
    }

    @Override
    public View onCreateContentView() {
        View v = createPopupById(R.layout.cardslist_pop);
        RecyclerView rv = v.findViewById(R.id.rv);
        v.findViewById(R.id.tv_cancel).setOnClickListener(v1 -> dismiss());
        rv.setLayoutManager(new LinearLayoutManager(Utils.getContext()));
        mDatas = new ArrayList<>();
        mAdapter = new DataEnterpriseAdapter(mDatas);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            mData = mAdapter.getData().get(position);
            BusinessInfoBean bean2 = mAdapter.getData().get(mLastIndex);
            if (mLastIndex != position) {
                mData.isSelect = true;
                bean2.isSelect = false;
            }
            mLastIndex = position;
            mAdapter.notifyDataSetChanged();
        });
        rv.setAdapter(mAdapter);
        TextView tv_sure = v.findViewById(R.id.tv_sure);
        tv_sure.setOnClickListener(v1 -> {
            isClick = true;
            dismiss();
        });
        mEtSs = v.findViewById(R.id.et_ss);
        mIvNull = v.findViewById(R.id.iv_null);
        mEtSs.addTextChangedListener(new MyZTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                Utils.sendMsg(Constants.EVENT_REFRESH_SEARCH, s.toString());


              /*  mSsDatas.clear();
                if (Utils.isEmpty(mEtSs.getText().toString())) {
                    for (int i = 0; i < mDatas.size(); i++) {
                        if (mDatas.get(i).isSelect) {
                            mLastIndex = i;
                        }
                    }
                    mAdapter.setNewInstance(mDatas);
                    mIvNull.setVisibility(mDatas.size() == 0 ? View.VISIBLE : View.GONE);
                } else {
                    for (BusinessInfoBean bankEntity : mDatas) {
                        bankEntity.isSelect = false;
                        if (bankEntity.payeeCompanyName.contains(mEtSs.getText().toString())) {
                            mSsDatas.add(bankEntity);
                        }
                    }
                    if (mSsDatas.size() != 0) {
                        mLastIndex = 0;
                        mSsDatas.get(0).isSelect = true;
                        mData = mSsDatas.get(0);
                    }
                    mIvNull.setVisibility(mSsDatas.size() == 0 ? View.VISIBLE : View.GONE);
                    mAdapter.setNewInstance(mSsDatas);
                    mAdapter.notifyDataSetChanged();
                }*/
            }
        });
        return v;
    }

    @Override
    protected Animation onCreateShowAnimation() {
        return AnimationHelper.asAnimation()
                .withTranslation(new TranslationConfig()
                        .from(Direction.BOTTOM))
                .toShow();
    }

    @Override
    protected Animation onCreateDismissAnimation() {
        return AnimationHelper.asAnimation()
                .withTranslation(new TranslationConfig()
                        .to(Direction.BOTTOM))
                .toDismiss();
    }


    public void show(View view) {
        isClick = false;
        if (mAdapter.getData().size() == 0) {
            mEtSs.setText("");
            mAdapter.setNewInstance(mDatas);
            mAdapter.notifyDataSetChanged();
        }
        setPopupGravity(Gravity.TOP);
        showPopupWindow(view);
    }

    public void showNewInstance(View view) {
        isClick = false;
        mLastIndex = 0;
        mAdapter.setNewInstance(mDatas);
        mAdapter.notifyDataSetChanged();
        setPopupGravity(Gravity.TOP);
        showPopupWindow(view);
    }
}